package com.lxw.sso.core.login;

import com.lxw.sso.core.conf.Conf;
import com.lxw.sso.core.store.SsoLoginStore;
import com.lxw.sso.core.store.SsoSessionIdHelper;
import com.lxw.sso.core.user.XxlSsoUser;
import com.lxw.sso.core.util.CookieUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xuxueli 2018-04-03
 */
public class SsoWebLoginHelper {

    /**
     * 获得userId然后存储对象值redis 返回SSO_SESSIONID
     *
     * @param response
     * @param sessionId
     * @param ifRemember    true: cookie not expire, false: expire when browser close （server cookie）
     * @param xxlUser
     */
    public static void login(HttpServletResponse response,
                             String sessionId,
                             XxlSsoUser xxlUser,
                             boolean ifRemember) {
        //解析seesionId获取userId
        String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
        if (storeKey == null) {
            throw new RuntimeException("parseStoreKey Fail, sessionId:" + sessionId);
        }
        //根据userId得到redisKey 后存储至redis
        SsoLoginStore.put(storeKey, xxlUser);
        //将sessionId返回至客户端
        CookieUtil.set(response, Conf.SSO_SESSIONID, sessionId, ifRemember);
    }

    /**
     * 删除redis中的用户信息,返回一个失效的cookie
     *
     * @param request
     * @param response
     */
    public static void logout(HttpServletRequest request,
                              HttpServletResponse response) {

        //得到cookie中的cookieSessionId
        String cookieSessionId = CookieUtil.getValue(request, Conf.SSO_SESSIONID);
        if (cookieSessionId==null) {
            return;
        }
        //得到userId
        String storeKey = SsoSessionIdHelper.parseStoreKey(cookieSessionId);
        if (storeKey != null) {
            //删除redis中的信息
            SsoLoginStore.remove(storeKey);
        }
        //返回一个失效的cookie
        CookieUtil.remove(request, response, Conf.SSO_SESSIONID);
    }



    /**
     * 检测请求的参数与cookie中 该项目对应的回话值,从而得到user对象或刷新过期时间,如果不存在或version不匹配则返回空对象
     *
     * @param request
     * @param response
     * @return
     */
    public static XxlSsoUser loginCheck(HttpServletRequest request, HttpServletResponse response){

        //从cookie中 根据cookie的名称得到cookie 然后得到cookie中的值 也就是cookieSessionId包含userId与version
        String cookieSessionId = CookieUtil.getValue(request, Conf.SSO_SESSIONID);

        //判断是否能根据userId 得到redis中存储的user对象,然后比较version 不存在则返回null,存在则判断过期时间 选择是否重置
        XxlSsoUser xxlUser = SsoTokenLoginHelper.loginCheck(cookieSessionId);
        if (xxlUser != null) {
            return xxlUser;
        }

        // redirect user

        //根据session名称重新得到新的cookie并设置为立即失效返回 从而删除cookie
        SsoWebLoginHelper.removeSessionIdByCookie(request, response);

        //从请求的参数中获取seesion配置  如果存在则再次判断userId对应的user对象与对象version,如果存在生成新的cookie
        String paramSessionId = request.getParameter(Conf.SSO_SESSIONID);
        xxlUser = SsoTokenLoginHelper.loginCheck(paramSessionId);
        if (xxlUser != null) {
            //生成新的cookie并设置失效时间为-1  即关闭浏览器就失效
            CookieUtil.set(response, Conf.SSO_SESSIONID, paramSessionId, false);
            return xxlUser;
        }

        return null;
    }


    /**
     * client logout, cookie only
     *
     * @param request
     * @param response
     */
    public static void removeSessionIdByCookie(HttpServletRequest request, HttpServletResponse response) {
        CookieUtil.remove(request, response, Conf.SSO_SESSIONID);
    }

    /**
     * get sessionid by cookie
     *
     * @param request
     * @return
     */
    public static String getSessionIdByCookie(HttpServletRequest request) {
        String cookieSessionId = CookieUtil.getValue(request, Conf.SSO_SESSIONID);
        return cookieSessionId;
    }


}
